$(document).ready(function(){
	$(".over").each(function(){
		$(this).bind("click",function(){addEdu(this);});
	});
	$(".saveinfo").each(function(){
		$(this).bind("click",function(){upEdu(this);});
	});
	$(".delinfo").each(function(){
		$(this).bind("click",function(){delEdu(this);});
	});
});
var d= new Date();
var year = d.getFullYear();
function addEdu(obj){
	//$(obj).parent().next().clone().prependTo($(obj).parent());
	if($("#addinfo").attr("id") == "addinfo"){
		$("#addinfo").parent().parent().remove();
	}
	var input = '<div class="eduInfo">';
	
	if($(obj).val() == '添加新大学'){
		input += '<p>类型： <select name="student_type"><option value="大学生">大学生</option><option value="研究生">研究生</option><option value="博士生">博士生</option></select>	</p>';
		input += '<p>大学： <input type="text" name="school_name" value=""/> <select name="regtime"><option value="0">入学时间</option>';
		
		for (var i=year;i>1960;i--){
			input += '<option value="'+i+'">'+i+'</option>';
		}
		input += '</select></p>';
		input += '<p>院系： <input type="text" name="department" value=""/> </p>';
		input += '<p><input type="hidden" name="school_type" value="1"/><input type="button" id="addinfo" class="input2" value="保存信息"/> <input type="button" class="delinfo input2" delid="0" value="删除此大学"/>	</p>';
	}else{
		input += '<p>高中： <input type="text" name="school_name"/> <select name="regtime"><option>入学年份</option>';
		for (var i=year;i>1960;i--){
			input += '<option value="'+i+'">'+i+'</option>';
		}
		input += '</select></p>';
		input += '<p><input type="hidden" name="school_type" value="2"/><input type="hidden" name="id" value="0"/><input type="button" id="addinfo" class="input2" value="保存信息"/> <input type="button" delid="0" class="delinfo input2" value="删除此高中"/></p>';
	}
	input += '<p></p></div>';
	$(obj).parent().after(input);
	$("#addinfo").bind("click",function(){saveEdu(this);});
	$(".delinfo").each(function(){
		$(this).bind("click",function(){delEdu(this);});
	});
}

function saveEdu(obj){
	var school_type = $(obj).prev().val();
	if(1 == school_type){
		var school_name = $(obj).parent().prev().prev().children("[name='school_name']").val();
		var student_type = $(obj).parent().prev().prev().prev().children("[name='student_type']").val();
		var regtime = $(obj).parent().prev().prev().children("[name='regtime']").val();
		var department = $(obj).parent().prev().children("[name='department']").val();
		$.post(APP+"/ajax/saveedu",{school_type:1 , school_name:school_name , student_type:student_type , regtime:regtime , department:department},
			function(data){
				if(data>0){
					
					$(obj).next().attr("delid",data)
					$(obj).after('<input type="button" class="saveinfo" value="修改信息"/>');
					$(obj).next().bind("click",function(){upEdu(this);});
					msg(obj,"信息添加成功");
					$(obj).remove();
				}else{
					msg(obj,"信息添加失败，你可以重试");
				}
			});
	}else{
		var school_name = $(obj).parent().prev().children("[name='school_name']").val();
		var regtime = $(obj).parent().prev().children("[name='regtime']").val();
		$.post(APP+"/ajax/saveedu",{school_type:2 , school_name:school_name , regtime:regtime },
			function(data){
				if(data>0){
					
					$(obj).next().attr("delid",data)
					$(obj).after('<input type="button" class="saveinfo" value="修改信息"/>');
					$(obj).prev().bind("click",function(){upEdu(this);});
					msg(obj,"信息添加成功");
					$(obj).remove();
				}else{
					msg(obj,"信息添加失败，你可以重试");
				}
			});
	}
}

function delEdu(obj){
	alertMessage(obj,"确定删除");
	$(".msgOk").bind("click",function(){delEduaction(obj);});
}

function delEduaction(obj){
	var id = $(obj).attr("delid");
	if(0== id){
		$(obj).parent().parent().remove();
		$("#messageBox").remove();
	}else{
		$.get(APP+"/ajax/deledu",{id:id},function(data){
			if("success" == data){
				$(obj).parent().parent().remove();
				$("#messageBox").remove();
			}else{
				msg(obj,"操作失败");
			}
		})
	}	
}
function upEdu(obj){
	var school_type = $(obj).prev().val();
	var id = $(obj).next().attr("delid");
	if(1 == school_type){
		var school_name = $(obj).parent().prev().prev().children("[name='school_name']").val();
		var student_type = $(obj).parent().prev().prev().prev().children("[name='student_type']").val();
		var regtime = $(obj).parent().prev().prev().children("[name='regtime']").val();
		var department = $(obj).parent().prev().children("[name='department']").val();
		$.post(APP+"/ajax/updateedu",{id:id , school_type:1 , school_name:school_name , student_type:student_type , regtime:regtime , department:department},
			function(data){
			alert(data);
				if("success" == data){
					msg(obj,"信息修改成功");
				}else{
					msg(obj,"信息修改失败，你可以重试");
				}
			});
	}else{
		var school_name = $(obj).parent().prev().children("[name='school_name']").val();
		var regtime = $(obj).parent().prev().children("[name='regtime']").val();
		$.post(APP+"/ajax/updateedu",{id:id , school_type:2 , school_name:school_name , regtime:regtime ,student_type:0 , department:0 },
			function(data){
				if("success" == data){
					msg(obj,"信息修改成功");
				}else{
					msg(obj,"信息修改失败，你可以重试");
				}
			});
	}
}